package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

import model.Database;
import model.Evento;
import model.Notifica;
import model.SearchEvent;
import controller.ActionHomeEvent;
import controller.Engine;

@SuppressWarnings("serial")
public class HomeEvent extends JPanel implements Observer{
	static JPanel event = new JPanel();
	static JPanel scrollContainer;
	static JPanel eventContainer = new JPanel();
	static JButton notifiche;
	static JScrollPane scroll;
	static JScrollPane scroll2;
	static Dimension schermo = Toolkit.getDefaultToolkit().getScreenSize();
	static SearchEvent e = new SearchEvent();
	JButton elimina;
	JButton modifica;
	static ActionListener action = new ActionHomeEvent();
	Vector<Notifica> notify;
	public static Vector<Integer> idUnread = new Vector<Integer>();
	int unread=0;

	public HomeEvent() {
		if (SearchEvent.evn.countObservers()!=0)
			SearchEvent.evn.deleteObservers();
		SearchEvent.evn.addObserver(this);
		startHomeEvent();
	}

	public void startHomeEvent() {
		event = new JPanel();
		event = this;
		event.setBackground(Color.white);
		event.setMinimumSize(new Dimension(655,190));
		scrollContainer = new JPanel();
		scrollContainer.setBackground(Color.white);
		scrollContainer.setPreferredSize(new Dimension (655,200));
		JPanel div =new JPanel();
		div.setBackground(Color.white);
		JPanel div1 = new JPanel();
		JButton addEvent = new JButton("Aggiungi un nuovo evento");
		JLabel selectedDate = new JLabel(HomeCalendar.day+"/"+HomeCalendar.month+"/"+HomeCalendar.year);

		setDescription();
		setNotify();

		notifiche = new JButton();
		notifiche.setName("notify");
		notifiche.setPreferredSize(new Dimension(220,30));
		notifiche.addActionListener(action);
		if (unread==0)
			notifiche.setText("Notifiche");
		else if (unread==1)
			notifiche.setText("Hai "+unread+" notifica da leggere");
		else
			notifiche.setText("Hai "+unread+" notifiche da leggere");
		addEvent.setName("addEvent");
		addEvent.addActionListener(action);
		addEvent.setPreferredSize(new Dimension (220,30));

		div1.setLayout(new BoxLayout(div1,BoxLayout.X_AXIS));
		div1.setPreferredSize(new Dimension (655,30));
		div1.setBackground(Color.white);
		div1.setAlignmentX(CENTER_ALIGNMENT);
		//div1.add(Box.createHorizontalGlue());
		div1.add(Box.createHorizontalStrut(0));
		div1.add(addEvent);
		div1.add(Box.createHorizontalStrut(40));
		div1.add(Box.createHorizontalGlue());
		div1.add(selectedDate);
		div1.add(Box.createHorizontalGlue());
		div1.add(Box.createHorizontalStrut(40));
		div1.add(notifiche);
		div1.add(Box.createHorizontalStrut(0));
		//div1.add(Box.createHorizontalGlue());

		div.setLayout(new BoxLayout(div,BoxLayout.Y_AXIS));
		div.add(div1);
		div.add(Box.createVerticalStrut(7));
		div.add(scrollContainer);
		event.add(div);
		event.setVisible(true);
	}

	public void setNotify(){
		notify = SearchEvent.getNotifyOfAccount(Database.Utente.getID());
		JPanel notifyContainer = new JPanel();
		notifyContainer.setLayout(new BoxLayout(notifyContainer,BoxLayout.Y_AXIS));
		notifyContainer.setBackground(Color.white);
		scroll2= new JScrollPane(notifyContainer,
				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scroll2.setPreferredSize(new Dimension(655,195));
		int pos=1;
		for (int i=notify.size()-1;i>=0;i--){ //scorro dall'ultimo elemento per avere le ultime notifiche aggiunte
			JTextArea notifyLabel =  new JTextArea(pos+"] "+notify.get(i).getCorpo());
			notifyLabel.setEditable(false);
			notifyLabel.setLineWrap(true);
			unread=0;
			if (notify.get(i).getFlag()==1){
				notifyLabel.setBackground(new Color(39,121,222,30));
				unread++;
				idUnread.add(notify.get(i).getNot_id());
			}
			else
				notifyLabel.setBackground(Color.white);
			notifyContainer.add(notifyLabel);
		}
		scroll2.setVisible(true);
	}

	public static void showNotify() {
		scrollContainer.remove(scroll);
		scrollContainer.add(scroll2, BoxLayout.X_AXIS);
		notifiche.setName("closeNotify");
		notifiche.setText("Chiudi notifiche");
		notifiche.setPreferredSize(new Dimension(220,30));
	}

	public static void closeNotify() {
		scrollContainer.remove(scroll2);
		scrollContainer.add(scroll, BoxLayout.X_AXIS);
	}

	public static void setDescription(){
		//System.out.println(HomeCalendar.day+"/"+HomeCalendar.month+"/"+HomeCalendar.year);
		// e = new SearchEvent();
		Vector<Evento> evento;
		if (Database.Utente.getLevel()!=512)
			evento= SearchEvent.getVectEventOfDate(HomeCalendar.day, HomeCalendar.month,HomeCalendar.year);
		else
			evento = SearchEvent.getAdminVectEventOfDate(HomeCalendar.day, HomeCalendar.month,HomeCalendar.year);
		eventContainer.setLayout(new BoxLayout(eventContainer,BoxLayout.Y_AXIS));
		//eventContainer.setBackground(Color.white);
		//eventContainer.setMinimumSize(new Dimension(734,10000)/*new Dimension((schermo.width/3)-40,10000)*/);
		eventContainer.setBackground(Color.white);
		scroll= new JScrollPane(eventContainer,
				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		//scroll.setMaximumSize(new Dimension(schermo.width/2,schermo.height/3));
		scroll.setPreferredSize(new Dimension(655,195));
		scrollContainer.add(scroll, BoxLayout.X_AXIS);
		eventContainer.removeAll();
		if (evento.size()==0){
			JLabel noEvent = new JLabel("Non sono presenti eventi per la data selezionata");
			eventContainer.add(noEvent);
		}
		else
		{
			for (int i=0;i<evento.size();i++){
				JLabel event_name = new JLabel(evento.get(i).getNome());
				event_name.setPreferredSize(new Dimension(10,20));
				event_name.setToolTipText(evento.get(i).getNome());
				JLabel ubica = new JLabel (evento.get(i).getLuogo());
				ubica.setPreferredSize(new Dimension(10,20));
				ubica.setToolTipText(evento.get(i).getLuogo());
				JTextArea descr=new JTextArea(evento.get(i).getDescr());
				descr.setEditable(false);
				JPanel pannelloEvento = new JPanel();
				JPanel div = new JPanel();
				JPanel dettagli = new JPanel();
				JPanel descrizione = new JPanel();
				JPanel descr_pan =new JPanel();
				div.setBackground(Color.white);
				dettagli.setBackground(Color.white);
				descrizione.setBackground(Color.white);
				descr_pan.setBackground(Color.white);
				JButton photo = new JButton();
				photo.add(Engine.setEventImage(evento.get(i).getType(),162,162));
				photo.setMargin(new Insets(5,0,5,0));
				photo.setFocusable(false);
				//photo.setEnabled(false);

				switch (SearchEvent.getLevelEventById(evento.get(i).getEvn_id())){
				case 0:{
					photo.setBackground(new Color(39,121,222,30));
					break;
				}
				case 1:{
					photo.setBackground(new Color(243,91,18,30));
					break;
				}
				default:photo.setBackground(new Color(185,220,255));
				}
				dettagli.setLayout(new BoxLayout(dettagli,BoxLayout.Y_AXIS));
				dettagli.add(Box.createHorizontalStrut(5));
				dettagli.add(new JLabel("Nome evento:"));
				dettagli.add(event_name);
				dettagli.add(Box.createHorizontalStrut(0));
				dettagli.add(new JLabel("Data: "+evento.get(i).getDay()));
				dettagli.add(new JLabel("Ora: "+evento.get(i).getTime()));
				dettagli.add(Box.createHorizontalStrut(0));
				dettagli.add(new JLabel("Luogo:"));
				dettagli.add(ubica);
				dettagli.add(Box.createHorizontalStrut(5));
				div.setLayout(new BoxLayout(div,BoxLayout.X_AXIS));
				div.add(Box.createHorizontalStrut(5));
				div.add(photo);
				div.add(Box.createHorizontalStrut(5));
				div.add(dettagli);
				div.add(Box.createHorizontalStrut(5));

				JLabel descr_txt =new JLabel("Descrizione");
				JPanel buttons = new JPanel();
				buttons.setBackground(Color.white);
				JButton modifica = new JButton("Modifica");
				JButton rimuovi = new JButton("Rimuovi");
				rimuovi.setName("delEvent");
				rimuovi.addActionListener(action);
				rimuovi.setMinimumSize(new Dimension (120,28));
				rimuovi.setPreferredSize(new Dimension (120,28));
				rimuovi.setMaximumSize(new Dimension (120,28));
				modifica.setName("modEvent");
				modifica.addActionListener(action);
				modifica.setMinimumSize(new Dimension (120,28));
				modifica.setPreferredSize(new Dimension (120,28));
				modifica.setMaximumSize(new Dimension (120,28));
				JComponent compId1 = new JComponent() {
				};
				compId1.setName(""+evento.get(i).getEvn_id());
				modifica.add(compId1);
				JComponent compId2 = new JComponent() {
				};
				compId2.setName(""+evento.get(i).getEvn_id());
				rimuovi.add(compId2);
				buttons.add(modifica);
				buttons.add(Box.createHorizontalStrut(20));
				buttons.add(rimuovi);
				//System.out.println("acc_id evento = "+evento.get(i).getAcc_id()+" acc_id utente = "+Database.Utente.getID());
				//System.out.println("data evento = "+evento.get(i).dd+"/"+evento.get(i).mm+"/"+evento.get(i).yy);
				//System.out.println("data odierna = "+HomeCalendar.thisdd+"/"+HomeCalendar.thismm+"/"+HomeCalendar.thisyy);
				if (Database.Utente.getLevel()!=512){
					if (evento.get(i).getAcc_id() != Database.Utente.getID()){
						modifica.setEnabled(false);
						rimuovi.setEnabled(false);
					}
					if (Integer.valueOf(evento.get(i).yy)<=Integer.valueOf(HomeCalendar.thisyy))
						if (Integer.valueOf(evento.get(i).mm)<=Integer.valueOf(HomeCalendar.thismm))
							if (Integer.valueOf(evento.get(i).dd)<=Integer.valueOf(HomeCalendar.thisdd)){
								modifica.setEnabled(false);
								rimuovi.setEnabled(false);
							}
				}
				descr_pan.setLayout(new BoxLayout(descr_pan,BoxLayout.X_AXIS));
				descr_pan.setAlignmentX(CENTER_ALIGNMENT);
				descr_pan.add(descr_txt);
				descrizione.setLayout(new BoxLayout(descrizione,BoxLayout.Y_AXIS));
				descrizione.setAlignmentX(LEFT_ALIGNMENT);
				descrizione.add(Box.createVerticalStrut(5));
				descrizione.add(descr_pan);
				JScrollPane scroll= new JScrollPane(descr,
						ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
						ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
				scroll.setPreferredSize(new Dimension (300,130));
				descr.setLineWrap(true);
				descrizione.add(scroll);
				descrizione.add(Box.createVerticalGlue());
				descrizione.add(buttons);
				descrizione.add(Box.createVerticalStrut(0));

				pannelloEvento.setLayout(new BoxLayout(pannelloEvento,BoxLayout.LINE_AXIS));
				pannelloEvento.setBackground(Color.white);
				pannelloEvento.add(div);
				pannelloEvento.add(Box.createHorizontalStrut(0));
				pannelloEvento.add(descrizione);
				pannelloEvento.add(Box.createHorizontalGlue());
				eventContainer.add(pannelloEvento);
			}
			eventContainer.setVisible(true);
		}
	}

	public static void uppdater() {
		SearchEvent.evn.uppdater();
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		event.removeAll();
		startHomeEvent();
		event.repaint();
		event.validate();
		//System.out.println("xx");
	}
}
